package com.jtcl.module.pojo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * 用户pojo
 *
 * @author 施锦庭
 * @since 2020-12-28 20:40
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ToString
@ApiModel("用户POJO")
public class User implements UserDetails {

    @ApiModelProperty("主键")
    private int id;
    @ApiModelProperty("用户名")
    private String username;
    @ApiModelProperty("密码")
    private String password;
    @ApiModelProperty("盐")
    private String salt;
    @ApiModelProperty("邮箱")
    private String email;
    @ApiModelProperty(value = "类型[0-普通用户; 1-超级管理员;]", allowableValues = "0,1")
    private int type;
    @ApiModelProperty(value = "状态[0-未激活; 1-已激活;]", allowableValues = "0,1")
    private int status;
    @ApiModelProperty("激活码")
    private String activationCode;
    @ApiModelProperty("？？？")
    private String headerUrl;
    @ApiModelProperty("创建时间")
    private Date createTime;

    /**
     * 获取某个用户所具备的权限的集合
     *
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<GrantedAuthority> list = new ArrayList<>();
        // 我们demo中只给用户加一个权限
        list.add(new GrantedAuthority() {
            @Override
            public String getAuthority() {
                switch (type) {
                    case 1:
                        return "ADMIM";// 管理员
                    default:
                        return "USER";// 普通与用户
                }
            }
        });
        return list;
    }

    /**
     * 返回true:  账号未过期.
     * 返回false: 账号过期.
     *
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 返回true:  账号未锁定.
     * 返回false: 账号锁定.
     *
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 返回true:  凭证未过期.
     * 返回false: 凭证过期.
     *
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 返回true: 账号可用.
     * 返回false: 账号不可用.
     *
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }

}
